package com.ctl.szmd.szmd.feature_wm.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.blankj.utilcode.util.LogUtils
import com.ctl.lib_common.extensions.singleClick
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.data.model.WmDataListModel
import com.ctl.szmd.szmd.feature_wm.databinding.FragmentWmWebBinding
import com.ctl.szmd.szmd.feature_wm.ui.adapter.wm.WmWebAttachmentAdapter
import com.ctl.szmd.szmd.feature_wm.ui.base.BaseWmBindingFragment
import com.ctl.szmd.szmd.feature_wm.ui.viewmodel.WmWebViewModel
import kotlinx.android.synthetic.main.fragment_wm_web.*
import kotlinx.android.synthetic.main.view_wm_tool_bar.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.koin.android.viewmodel.ext.android.viewModel


class WmWebFragment : BaseWmBindingFragment<FragmentWmWebBinding>() {

    companion object {
        const val TITLE = "title"
        const val URL = "url"
        const val INFO_ID = "InfoId"
        const val FROM_WHERE = "fromWhere"
        const val ATTACHMENT_COUNT = "AttachmentCount"
        const val VISIT_TYPE_NUM = "VisitTypeNum"
    }

    private val viewModel: WmWebViewModel by viewModel()

    private val mAdapter by lazy { WmWebAttachmentAdapter() }

    override fun getLayoutId(): Int {
        return R.layout.fragment_wm_web
    }

    @ExperimentalCoroutinesApi
    override fun initView() {
        binding.vm = viewModel
        lifecycle.addObserver(viewModel)
        binding.recyclerView.adapter = mAdapter

        toolbar_left.visibility = View.VISIBLE
        toolbar_left.singleClick { findNavController().navigateUp() }
        toolbar_title.text = arguments?.getString(TITLE, "")
        val url = arguments?.getString(URL, "")
        val infoId = arguments?.getString(INFO_ID, "")
        val fromWhere = arguments?.getString(FROM_WHERE, "")
        val visitTypeNum = arguments?.getInt(VISIT_TYPE_NUM, 0) ?: 0
        val attachmentCount = arguments?.getInt(ATTACHMENT_COUNT, 0) ?: 0
        if (!url.isNullOrBlank()) {
            if (url.startsWith("http")) {
                web_view.loadUrl(url)
            } else {
                web_view.loadData(url, "text/html; charset=UTF-8", null)
            }
        }
        web_view.setDownloadListener { _url, _, _, _, _ ->
            LogUtils.i(_url)
        }
        if (visitTypeNum != 0 && infoId != null) {
            viewModel.getInfoVisitCountStatistics(visitTypeNum, infoId)
        }

        var path: String? = null
        when (fromWhere) {
            "巾帼心向党" -> path = "/api/InfoFaceParty/InfoCommonDetail"
            "妇联头条" -> path = "/api/InfoWomenHeadline/InfoCommonDetail"
            "基层风采" -> path = "/api/InfoBasicStyle/InfoCommonDetail"
            "最美家庭" -> path = "/api/InfoPerfectFamily/InfoCommonDetail"
            "巾帼文明岗" -> path = "/api/InfoDoBestPosition/InfoCommonDetail"
            "巾帼志愿者" -> path = "/api/InfoVolunteer/InfoCommonDetail"
            "爱心妈妈" -> path = "/api/InfoCaringMama/InfoCommonDetail"
            "法律常识" -> path = "/api/InfoLegalKnowledge/InfoCommonDetail"
            "技能培训" -> path = "/api/InfoSkillTraining/InfoCommonDetail"
            "小额贷款" -> path = "/api/InfoPettyLoan/InfoCommonDetail"
            "创新创业" -> path = "/api/InfoEntrepreneurship/InfoCommonDetail"
            "家长学校" -> path = "/api/InfoParentSchool/InfoCommonDetail"
            "妇女儿童活动中心" -> path = "/api/InfoActivityCenter/InfoCommonDetail"
            "妇女儿童活动之家" -> path = "/api/InfoActivityHome/InfoCommonDetail"
            "儿童快乐家园" -> path = "/api/InfoChildrenHome/InfoCommonDetail"
            "学习资料" -> path = "/api/StudyFulian/StudyMaterialDetail"
            "使用帮助" -> path = "/api/InfoFulianHelp/InfoCommonDetail"
        }

        if (attachmentCount > 0 && !path.isNullOrBlank() && !infoId.isNullOrBlank() && !fromWhere.isNullOrBlank()) {
            val map = mutableMapOf<String, String>()
            map["InfoId"] = infoId
            map["MaterialId"] = infoId
            map["ContentId"] = infoId
            viewModel.getInfoWebViewAttachmentList(path, map)
                .observe(viewLifecycleOwner, Observer {
                    mAdapter.submitList(it)
                })
            mAdapter.listener = View.OnClickListener {
                val dataListModel = (it.tag as WmDataListModel)
                if (dataListModel.attachmentName != null) {
                    val bundle = Bundle()
                    bundle.putString(FileDisplayFragment.PATH, dataListModel.attachmentNetPath!!)
                    bundle.putString(FileDisplayFragment.FILE_NAME, dataListModel.attachmentName!!)
                    findNavController().navigate(R.id.fileDisplayFragment, bundle)
                }
            }
        }

    }
}